import { clear_gun, get_gun } from 'base/api';
import { info_rm } from 'use/info';
import { do_event } from './event';
import { s_psn, g_db } from 'db/state';
import { put_log } from 'base/ctl/log';
import { uDb, cline, obj_str } from 'sui';

const do_scanout = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描外圈轴承编码");

    clear_gun();

    s_psn(psn);
    g_db.s_msn("");
    g_db.s_next("");

    umd.mset({
        "psn": psn,
        "msn": "",
        "next": "",
    });

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    if (psn.length > 10) {
        return herr("条码位数错误");
    }

    uDb(null, "psn/get",
        {
            line: cline(),
            k: psn,
        },
        r => {
            const old_task = g_db.task();
            const task = obj_str(r, "task");

            if (old_task && old_task !== task) {
                return herr(`此编码绑定的工单为: ${task}, 与当前工单不匹配`);
            }

            if (!old_task) {
                g_db.s_task(task);
                umd.set("task", task);
            }

            const next = obj_str(r, "insn");
            g_db.s_next(next);
            umd.set("next", next);

            do_event("step_over", umd, watch);
            watch.startWatch();
        },
        e => herr("此编码不存在, 请换件并重新扫码"));
};

const do_ngout = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描外圈轴承编码");

    clear_gun();

    s_psn(psn);
    g_db.s_msn("");
    g_db.s_next("");

    umd.mset({
        "psn": psn,
        "msn": "",
        "next": "",
    });

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    if (psn.length > 10) {
        return herr("条码位数错误");
    }

    uDb(null, "psn/get",
        {
            line: cline(),
            k: psn,
        },
        r => {
            const task = obj_str(r, "task");
            g_db.s_task(task);
            umd.set("task", task);

            const next = obj_str(r, "insn");
            g_db.s_next(next);
            umd.set("next", next);

            do_event("step_over", umd, watch);
            watch.startWatch();
        },
        e => herr("此编码不存在, 请换件并重新扫码"));
};

const do_outpsn = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描外圈轴承编码");

    clear_gun();

    s_psn(psn);

    umd.mset({
        "psn": psn,
    });

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    if (psn.length > 10) {
        return herr("条码位数错误");
    }

    uDb(null, `file/rm/box_${g_db.box()}`,
        {
            line: cline(),
            sn: psn,
        },
        r => {
            umd.mset({
                err: "",
            });

            info_rm(umd, psn);

            watch.startWatch();
        },
        e => herr("产品号绑定箱号出错"));
};

export {do_outpsn, do_ngout, do_scanout};
